package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.Expert;
import com.css.fxfzaqbz.modules.data.entity.Report;
import com.css.fxfzaqbz.modules.data.repository.ExpertRepository;
import com.css.fxfzaqbz.modules.data.vo.ExpertVo;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ExpertRepositoryImpl implements ExpertRepository {

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    JdbcTemplate jdbcTemplate;
    @Override
    public void saveExpert(Expert expert) {
        String sql = "insert into sys_expert (id,zj_id,zj_name,status,sr_id,opinion) values(" +
                ":id,:zjId," +
                ":zjName,:status," +
                ":srId,:opinion)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(expert);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public void updateStatus(String s, String srId) {
        String sql="update sys_expert set status ='"+s+"' where sr_id='"+srId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<ExpertVo> getAllExpert(Report report) {
        StringBuilder sql = new StringBuilder(" SELECT e.*,r.* FROM sys_expert e LEFT JOIN sys_report r on e.sr_id=r.sr_id ");

         StringBuilder orderSql = new StringBuilder(" order by r.create_time");

        StringBuilder whereSql = new StringBuilder(" where r.del_flag = '" + YNEnum.N.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(report.getProvinceId())) {
            whereSql.append(" and r.province_id ='"+report.getProvinceId()+"'");
        }

        if (!PlatformObjectUtils.isEmpty(report.getCityId())) {
            whereSql.append(" and r.city_id  ='" + report.getCityId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(report.getCountiesId())) {
            whereSql.append(" and r.counties_id ='" + report.getCountiesId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(report.getSrName())) {
            whereSql.append(" and r.sr_name like '%" + report.getSrName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(report.getSrType())) {
            whereSql.append(" and r.sr_type like '%" + report.getSrType() + "%'");
        }
        String userID = PlatformSessionContext.getUserID();
//获取当前登录人


//判断当前登录人是属于 初级审核人员 还是部门审核人员 还是领导审核人员
//一个登录人可以是多个 角色 比如在一个活动中是初级  在另一个活动中是部门
//


        whereSql.append(" and e.zj_id = '"+userID+"' ");


        sql.append(whereSql).append(orderSql).toString();
        List<ExpertVo> expertVoList = null;
        try {
            expertVoList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ExpertVo.class));
        } catch (DataAccessException e) {
            return null;
        }
        return expertVoList;
    }

    @Override
    public void updateExpert(Expert expert) {
        String sql = "update sys_expert set" +
                " status = :status, " +
                " opinion = :opinion," +
                " ps_time = :psTime" +
                " where id = '" + expert.getId() + "'";


        SqlParameterSource source = new BeanPropertySqlParameterSource(expert);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<Expert> getAllExpertBySrId(String srId) {
        String sql = "select * from sys_expert where sr_id = '" + srId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Expert.class));
    }

    @Override
    public Expert getById(String id) {
        String sql = "select * from sys_expert where id = '" + id + "';";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Expert.class));
    }
}
